{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/njuciairs/wangshuai/test/FinancialNagetiveEntityJudge'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from evaluation.evaluate import evaluate\n",
    "from data_utils.basic_data import load_train_val_dataset,load_basic_dataset\n",
    "from results_process.regulizer import remove_nine,remove_short_entity\n",
    "from results_process.utils import load_model_rs\n",
    "from results_process.bert_entity_model import reduce_rs_by_id\n",
    "from functools import reduce\n",
    "import numpy as np\n",
    "from data_utils.bert_multi_class_data import get_train_val_data_loader, get_test_loader,TestEntityDataset\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_df = load_model_rs(model_name='BertMultiClass',version_id=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_df = load_basic_dataset(split='test')\n",
    "test_dataset = TestEntityDataset(test_df, max_len=400)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "texts = [sample.text for sample in test_dataset]\n",
    "entity = [t.split('[SEP]')[0][5:] for t in texts]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_df['key_entity'] = entity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_map = {}\n",
    "for id,label,entity in raw_df.values:\n",
    "    if id not in rs_map:\n",
    "        rs_map[id] = ([label],[entity])\n",
    "    else:\n",
    "        rs_map[id][0].append(label)\n",
    "        rs_map[id][1].append(entity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "items = []\n",
    "for k,v in rs_map.items():\n",
    "    labels,entities = v\n",
    "    senti = int(np.mean(labels) >= 1)\n",
    "    keys = []\n",
    "    for l,e in zip(labels,entities):\n",
    "        if l==2:\n",
    "            keys.append(e)\n",
    "    key_entity = ';'.join(keys)\n",
    "    if len(keys)==0 or senti==0:\n",
    "        key_entity = np.nan\n",
    "    items.append((k,senti,key_entity))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>negative</th>\n",
       "      <th>key_entity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>f3b61b38</td>\n",
       "      <td>1</td>\n",
       "      <td>小资钱包;资易贷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>84b12bae</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>6abf4a82</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>8d076785</td>\n",
       "      <td>1</td>\n",
       "      <td>易捷金融;宜贷网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>d65a1577</td>\n",
       "      <td>1</td>\n",
       "      <td>贵金属</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4995</td>\n",
       "      <td>cf4782db</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4996</td>\n",
       "      <td>36895a84</td>\n",
       "      <td>1</td>\n",
       "      <td>瑞丰寄售行;融信贷;爱贷金服</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4997</td>\n",
       "      <td>46e3ae0b</td>\n",
       "      <td>1</td>\n",
       "      <td>易捷公司;摩尔龙;宜贷网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4998</td>\n",
       "      <td>31e49e9d</td>\n",
       "      <td>1</td>\n",
       "      <td>小资钱包;资易贷;北京正聚源公司</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4999</td>\n",
       "      <td>77bc7d45</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5000 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            id  negative        key_entity\n",
       "0     f3b61b38         1          小资钱包;资易贷\n",
       "1     84b12bae         0               NaN\n",
       "2     6abf4a82         0               NaN\n",
       "3     8d076785         1          易捷金融;宜贷网\n",
       "4     d65a1577         1               贵金属\n",
       "...        ...       ...               ...\n",
       "4995  cf4782db         0               NaN\n",
       "4996  36895a84         1    瑞丰寄售行;融信贷;爱贷金服\n",
       "4997  46e3ae0b         1      易捷公司;摩尔龙;宜贷网\n",
       "4998  31e49e9d         1  小资钱包;资易贷;北京正聚源公司\n",
       "4999  77bc7d45         0               NaN\n",
       "\n",
       "[5000 rows x 3 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_df = pd.DataFrame(items,columns=['id','negative','key_entity'])\n",
    "raw_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "#去重：把更短的去掉\n",
    "import numpy as np\n",
    "def remove_short_entity_by_long(entity_str):\n",
    "    \"\"\"\n",
    "    除去key_entity中同一实体的较短名称\n",
    "    :param entity_str:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    if not isinstance(entity_str, str):\n",
    "        return entity_str\n",
    "    entities = entity_str.split(';')\n",
    "    states = np.ones(len(entities))\n",
    "    for i, e in enumerate(entities):\n",
    "        for p in entities:\n",
    "            if e in p and len(e) < len(p):\n",
    "                print('removed %s by %s'%(e,p))\n",
    "                states[i] = 0\n",
    "    rs = []\n",
    "    for i, e in enumerate(entities):\n",
    "        if states[i] == 1:\n",
    "            rs.append(e)\n",
    "    rs = ';'.join(rs)\n",
    "    return rs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_trans_map():\n",
    "    from data_utils.basic_data import load_basic_dataset\n",
    "    train_df = load_basic_dataset('train')\n",
    "    srcs = train_df['entity'].map(lambda x :list(str(x).split(';')))\n",
    "    dests =  train_df['key_entity'].map(lambda x :list(str(x).split(';')))\n",
    "    trans_map = {}\n",
    "    for srcs,dests in list(zip(srcs,dests)):\n",
    "        for src in srcs:\n",
    "            if src == '':\n",
    "                continue\n",
    "            for e in srcs:\n",
    "                if e== '':\n",
    "                    continue\n",
    "                if (src in e or e in src) and e!=src:\n",
    "                    if src in dests:\n",
    "                        trans_map[src+'-'+e] = src\n",
    "                        trans_map[e+'-'+src] = src\n",
    "                    if e in dests:\n",
    "                        trans_map[src+'-'+e] = e\n",
    "                        trans_map[e+'-'+src] = e\n",
    "    return trans_map\n",
    "def trans_keys(trans_map,entity_str):\n",
    "    if not isinstance(entity_str,str):\n",
    "        return entity_str\n",
    "    es = list(filter(lambda x:str(x).strip()!='',entity_str.split(';')))\n",
    "    rs = set()\n",
    "    for e in es:\n",
    "        finded = False\n",
    "        for y in es:\n",
    "            if e+'-'+y in trans_map and e!=y:\n",
    "                rs.add(trans_map[e+'-'+y])\n",
    "                finded = True\n",
    "        if not finded:\n",
    "            rs.add(e)\n",
    "    if len(rs) > 0:\n",
    "        rs = ';'.join(list(rs))\n",
    "    else:\n",
    "        rs = np.nan\n",
    "    return rs\n",
    "trans_map = get_trans_map()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_df = raw_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "removed 麒麟金融 by 麒麟金融集团有限公司\n",
      "removed 优库速购 by 深圳优库速购\n",
      "removed 蜜蜂 by 蜜蜂财富\n",
      "removed 瑞波 by 瑞波币\n",
      "removed 联璧 by 联璧金融\n",
      "removed 随手记 by 随手记钱包\n",
      "removed 钱包 by 随手记钱包\n",
      "removed 天天投 by 天天投金融\n",
      "removed 商银信支付 by 商银信支付服务有限公司\n",
      "removed 时贷 by 大时贷\n",
      "removed 蜜蜂 by 蜜蜂财富\n",
      "removed 国金所 by 滨海国金所\n",
      "removed 米融 by 易米融\n",
      "removed 卡宝 by 卡宝典\n",
      "removed 玖富 by 北京玖富集团\n",
      "removed 创赢投资 by 上海宝银创赢投资管理有限公司\n",
      "removed 宝银创赢 by 上海宝银创赢投资管理有限公司\n",
      "removed 富通天下 by 深圳市富通天下投资管理有限公司\n",
      "removed 天下投 by 深圳市富通天下投资管理有限公司\n",
      "removed 深圳市富通 by 深圳市富通天下投资管理有限公司\n",
      "removed 无忧借条 by ????无忧借条\n",
      "removed 汇信 by 桐乡市汇信小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 桐乡市汇信小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 嘉兴市南湖区银丰小额贷款有限公司\n",
      "removed 优宝 by 优宝汇\n",
      "removed 麒麟金融 by 麒麟金融集团有限公司\n",
      "removed 壹佰金融 by 深圳壹佰金融\n",
      "removed 青岛九州商品交易中心 by 青岛九州商品交易中心有限公司\n",
      "removed 钱宝 by 出钱宝\n",
      "removed 资易贷 by 资易贷北京金融信息服务有限公\n",
      "removed 雪橙 by 雪橙金服\n",
      "removed 凯福德 by KFD凯福德\n",
      "removed 仁和融兴 by 青岛仁和融兴投资有限公司\n",
      "removed 二元期权 by 金盛二元期权\n",
      "removed 资易贷 by 资易贷(北京）金融信息有限公司公司\n",
      "removed 澳瑞克 by OracleFX澳瑞克\n",
      "removed 中南大宗 by 中南大宗商品电子商务有限公司\n",
      "removed 惠金服 by 城惠金服\n",
      "removed 富民投资 by 富民投资网\n",
      "removed 小资钱包 by ????小资钱包\n",
      "removed 资易贷 by 小资钱包公司(资易贷平台)\n",
      "removed 小资钱包 by 小资钱包公司(资易贷平台)\n",
      "removed 兴业财富 by 北方兴业财富\n",
      "removed 北京华澳融信 by 北京华澳融信（余盆网）\n",
      "removed 余盆网 by 北京华澳融信（余盆网）\n",
      "removed 智云 by 智云金融\n",
      "removed 智融财富 by 深圳市智融财富电商投资有限公司\n",
      "removed 贵金属 by 贵金属投资\n",
      "removed 麒麟金融 by 麒麟金融集团有限公司\n",
      "removed 上海易贷网 by 上海易贷网金融信息服务有限公司\n",
      "removed 中吴财富 by 上海中吴财富投资管理集团有限公司\n",
      "removed 钱宝 by 钱宝财\n",
      "removed 嘉盛 by 华嘉盛\n",
      "removed 玖财 by 玖财通\n",
      "removed 节节贷 by 广西弘尚节节贷网络信息服务集团有限公司\n",
      "removed 北京华澳翼时代 by 北京华澳翼时代信息技术有限责任公司\n",
      "removed 北京华澳融信 by 北京华澳融信国际投资管理咨询有限公司\n",
      "removed E都市钱包 by  E都市钱包\n",
      "removed 市大时代 by 深圳市大时代资产管理有限公司\n",
      "removed 时贷 by 大时贷\n",
      "removed 日融财富 by 宁波日融财富投资管理有限公司\n",
      "removed 海象理财 by 北京海象理财\n",
      "removed 零钱罐 by 零钱罐APP\n",
      "removed 钱吧 by 弹钱吧\n",
      "removed 誉东方投资管理 by 深圳市誉东方投资管理平台\n",
      "removed 东方投 by 深圳市誉东方投资管理平台\n",
      "removed 东方投 by 誉东方投资管理\n",
      "removed 红太阳 by 湖南红太阳电源新材料股份有限公司\n",
      "removed 大宗商品交易 by 河北滨海大宗商品交易市场服务有限公司\n",
      "removed 大宗商品交易 by 河北滨海大宗商品交易市场\n",
      "removed 河北滨海大宗商品交易市场 by 河北滨海大宗商品交易市场服务有限公司\n",
      "removed 资易贷 by 海淀资易贷平台\n",
      "removed 两只老虎 by 两只老虎理财\n",
      "removed 老虎理财 by 两只老虎理财\n",
      "removed 海象理财 by 北京海象理财\n",
      "removed 贷联盟 by 新贷联盟\n",
      "removed 联璧 by 联璧金融\n",
      "removed 钱海湾金融 by 钱海湾金融公司\n",
      "removed 钱海湾 by 钱海湾金融公司\n",
      "removed 钱海湾 by 钱海湾金融\n",
      "removed 时贷 by 森昊好时贷\n",
      "removed tbc by tbc（海湾资本）澳大利亚asic\n",
      "removed 渤海商品交易 by 天津渤海商品交易所\n",
      "removed 冠群驰骋 by ????冠群驰骋\n",
      "removed 冠群驰骋 by 冠群驰骋商务信息咨询（天津）有限公司\n",
      "removed 联金所 by 联金所康\n",
      "removed 米咖 by 米咖网\n",
      "removed 中吴财富 by 上海中吴财富投资管理集团有限公司\n",
      "removed 随行付 by 随行付支付有限公司\n",
      "removed 随行付 by 随行付支付\n",
      "removed 随行付支付 by 随行付支付有限公司\n",
      "removed 亿金融 by 优亿金融\n",
      "removed 小额贷 by 广州市恒隆小额贷款有限公司\n",
      "removed 小额贷 by 恒隆小额贷款\n",
      "removed 小额贷 by 小额贷款有限公司\n",
      "removed 恒隆小额贷款 by 广州市恒隆小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 广州市恒隆小额贷款有限公司\n",
      "removed 资易贷 by 资易贷（北京）金融信息服务有限公司旗\n",
      "removed 金易融 by 金易融（北京）网络科技有限公司\n",
      "removed 唐小僧 by 唐小僧之后\n",
      "removed 小额贷 by 中合华惠农村小额贷款有限公司\n",
      "removed 小额贷 by 小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 中合华惠农村小额贷款有限公司\n",
      "removed 和耕传承基金 by 和耕传承基金销售有限公司控股\n",
      "removed 以太坊 by 以太坊ETH\n",
      "removed 翱晟投资 by 台州翱晟投资公司\n",
      "removed 老虎金融 by 老虎金融信息服务（北京）有限公司\n",
      "removed 冠群驰骋 by 冠群驰骋投资管理(北京)有限公司\n",
      "removed 冠群驰骋 by 冠群驰骋投资关联(北京)有限公司\n",
      "removed 星投资 by 中子星投资\n",
      "removed 星投资 by 中子星投资有限公司\n",
      "removed 中子星投资 by 中子星投资有限公司\n",
      "removed 钱宝 by 钱宝财\n",
      "removed 创投网 by 阿里创投网络科技（北京）有限公司\n",
      "removed 一号家居网 by 一号家居网装饰公司\n",
      "removed 上海文化产权交易所 by 上海文化产权交易所股份有限公司\n",
      "removed 仁和融兴 by 青岛仁和融兴投资有限公司\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 宜贷网 by ?宜贷网\n",
      "removed 点融 by 点融网\n",
      "removed 汇聚财富 by 上海汇聚财富\n",
      "removed 海贷金服 by 海贷金服体验金\n",
      "removed 海贷 by 海贷金服\n",
      "removed 海贷 by 海贷金服体验金\n",
      "removed 和信 by 资和信\n",
      "removed 易商通 by 北京易商通科技有限公司\n",
      "removed 房融 by 房融所\n",
      "removed 钱宝 by 钱宝财\n",
      "removed 渤海创投 by ?渤海创投子公司智慧蜂巢\n",
      "removed 齐鲁商品 by 齐鲁商品交易中心\n",
      "removed 嘉盛 by 嘉盛国际\n",
      "removed 深圳光彩 by 深圳光彩投资控股集团有限公司\n",
      "removed 凯富 by 凯富K\n",
      "removed 资易贷 by 资易贷（小资钱包）\n",
      "removed 小资钱包 by 资易贷（小资钱包）\n",
      "removed 山东海倍电子商务 by 山东海倍电子商务股份有限公司\n",
      "removed 理财帝 by 理财帝国\n",
      "removed 沃德 by 沃德斯国际\n",
      "removed 山东海倍电子商务 by 山东海倍电子商务股份有限公司\n",
      "removed 渤海创投 by 渤海创投集团通\n",
      "removed 高盛国际 by gs-forex高盛国际\n",
      "removed 银通投资 by 中润银通投资北京有限公司\n",
      "removed 中银 by 中银消费金融有限公司\n",
      "removed 中银 by 中银消费金融\n",
      "removed 中银消费金融 by 中银消费金融有限公司\n",
      "removed 汇聚财富 by 上海汇聚财富投资\n",
      "removed 米融 by 易米融\n",
      "removed 御顺金融 by 成都御顺金融贷款公司\n",
      "removed 渤海商品交易 by 天津渤海商品交易所\n",
      "removed 渤海商品交易 by 渤海商品交易所\n",
      "removed 渤海商品交易所 by 天津渤海商品交易所\n",
      "removed 瑞财 by 恒瑞财富\n",
      "removed 中海投资 by 上海中海投资产管理有限公司\n",
      "removed 中海投 by 中海投资\n",
      "removed 中海投 by 上海中海投资产管理有限公司\n",
      "removed 网易 by 网易公司\n",
      "removed 日融财富 by 宁波日融财富投资管理有限公司\n",
      "removed 小资 by 小资钱包\n",
      "removed 富民投资 by 富民投资网\n",
      "removed 宜信 by 宜信惠民\n",
      "removed 宜信 by 宜信普惠\n",
      "removed 宜贷网 by 宜贷网（原易贷网）\n",
      "removed 易贷网 by 宜贷网（原易贷网）\n",
      "removed 天合 by 安徽天合联盟科技有限公司\n",
      "removed 天合 by 天合联盟\n",
      "removed 天合联盟 by 安徽天合联盟科技有限公司\n",
      "removed 嘉盛 by 嘉盛国际\n",
      "removed 中银 by 中银消费金融有限公司\n",
      "removed 中银 by 中银消费金融\n",
      "removed 中银消费金融 by 中银消费金融有限公司\n",
      "removed bc by bch\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 节节贷 by 广西弘尚节节贷集团(节节资本)\n",
      "removed 节节贷 by 广西弘尚节节贷集团\n",
      "removed 广西弘尚节节贷集团 by 广西弘尚节节贷集团(节节资本)\n",
      "removed bc by bch\n",
      "removed 宜贷网 by 宜贷网（原易贷网）\n",
      "removed 易贷网 by 宜贷网（原易贷网）\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 钱宝 by 钱宝财\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 小额贷 by 万源农村小额贷款有限公司与沈秋云\n",
      "removed 小额贷 by 小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 万源农村小额贷款有限公司与沈秋云\n",
      "removed 信e贷 by 久信e贷\n",
      "removed 易商通 by 北京易商通科技有限公司\n",
      "removed 易商通 by 北京易商通科技有限公司\n",
      "removed 深圳高新 by 深圳高新盛\n",
      "removed 高新盛 by 深圳高新盛\n",
      "removed 简贷 by 易简贷\n",
      "removed 翱晟投资 by 台州翱晟投资公司\n",
      "removed 银网贷 by 超银网贷\n",
      "removed 宜贷网 by ?宜贷网\n",
      "removed 深圳高新 by 深圳高新盛创投电子商务有限公司\n",
      "removed 高新盛 by 深圳高新盛创投电子商务有限公司\n",
      "removed 懒财主 by 前海懒财主金融\n",
      "removed 懒财主 by 海懒财主金融信息服务（深圳）有限公司\n",
      "removed 麒麟金融 by 麒麟金融集团有限公司\n",
      "removed 长征财富 by 长征财富资产管理有限公司宝应支公司\n",
      "removed 易贷金融 by  (北京)资易贷金融信息服务有限公司\n",
      "removed 资易贷 by 北京资易贷公司（小资钱包)\n",
      "removed 小资钱包 by 北京资易贷公司（小资钱包)\n",
      "removed 银通投资 by 中润银通投资(北京)有限公司\n",
      "removed 易贷金融 by  (北京)资易贷金融信息服务有限公司\n",
      "removed fx by onefx\n",
      "removed fx by fx福克斯\n",
      "removed 小资钱包 by  资易贷(小资钱包）\n",
      "removed 信和大金融 by 传信和大金融\n",
      "removed 乐贷 by 网乐贷\n",
      "removed 麒麟金融 by 麒麟金融集团有限公司\n",
      "removed 钱包 by 钱包网\n",
      "removed 华金融 by 高仕华金融\n",
      "removed 米融 by 易米融\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 易贷金融 by  (北京)资易贷金融信息服务有限公司\n",
      "removed 资易贷 by 资易贷（北京）金融信息服务公司\n",
      "removed 三农金服 by 深圳三农金服\n",
      "removed 云财富 by 外滩云财富\n",
      "removed 世纪贷 by 世纪贷互联网金融服务有限公司\n",
      "removed 随行付 by 随行付支付有限公司山西分公司\n",
      "removed 随行付 by 随行付支付\n",
      "removed 随行付支付 by 随行付支付有限公司山西分公司\n",
      "removed 聚财猫 by ????聚财猫\n",
      "removed 滴水贷 by ????滴水贷\n",
      "removed 中赢投 by 中赢投资\n",
      "removed 易贷金融 by （北京）资易贷金融信息服务有限公司\n",
      "removed 华夏信财 by 华夏信财信息咨询（上海）有限公司芜湖分公司\n",
      "removed 国润 by 江西国润\n",
      "removed 亚太投资 by 北京亚太投资\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 恒宇 by 恒宇天泽\n",
      "removed 圣盈信 by 圣盈信CIFS\n",
      "removed 北京华澳融信 by 北京华澳融信国际投资管理咨询有限公司\n",
      "removed 融通资产 by 北京圆融通资产管理有限公司\n",
      "removed 汇金方格 by 汇金方格（北京）投资管理有限公司\n",
      "removed 盛付通 by 上海盛付通电子支付服务有限公司陕西分公司\n",
      "removed 小额贷 by 金久农村小额贷款有限公司\n",
      "removed 小额贷 by 小额贷款有限公司\n",
      "removed 小额贷款有限公司 by 金久农村小额贷款有限公司\n",
      "removed 重庆赛伯乐盈科 by 重庆赛伯乐盈科股权投资基金管理有限公司\n",
      "removed 新余铭沃 by 新余铭沃投资管理中心\n",
      "removed 赛伯乐绿科 by 深圳赛伯乐绿科投资管理有限公司\n",
      "removed 小资钱包 by 北京资易贷金融信息服务有限公司（简称小资钱包）\n",
      "removed 粤融泰富 by 广东粤融泰富网络信息服务有限公司\n",
      "removed 大宗商品交易 by 河北滨海大宗商品交易市场\n",
      "removed 玖富 by 玖富?投诉量最多\n",
      "removed 海钜信达 by 深圳市海钜信达投资发展有限公司\n",
      "removed 陆金所 by 西部陆金所\n",
      "removed 贵金属 by nine\n",
      "removed 诺诺镑客 by nine\n",
      "removed Ｅ镑客 by nine\n",
      "removed 稳盈宝 by nine\n",
      "removed 火币 by nine\n",
      "removed bc by nine\n",
      "removed 小葱 by nine\n",
      "removed 支付宝 by nine\n",
      "removed 支付宝 by nine\n",
      "removed 中业兴融 by nine\n",
      "removed 好e贷 by nine\n",
      "removed 金贝 by nine\n",
      "removed cp by nine\n",
      "removed kci by nine\n",
      "removed 南京银行 by nine\n",
      "removed 聚才道 by nine\n",
      "removed 和中 by nine\n",
      "removed 借呗 by nine\n",
      "removed 出钱宝 by nine\n",
      "removed 君享金融 by nine\n",
      "removed 花呗 by nine\n",
      "removed 金盛二元期权 by nine\n",
      "removed 米咖网 by nine\n",
      "removed 优品 by nine\n",
      "removed 广发财富 by nine\n",
      "removed 盒子支付 by nine\n",
      "removed 超爱财 by nine\n",
      "removed 无忧车贷 by nine\n",
      "removed 秒钱 by nine\n",
      "removed 华金融 by nine\n",
      "removed 大宗商品交易 by nine\n",
      "removed 汇投资 by nine\n",
      "removed 汇投资 by nine\n",
      "removed 发隆金融 by nine\n",
      "removed 乐金所 by nine\n",
      "removed 双雄 by nine\n",
      "removed 光汇云油 by nine\n",
      "removed 元宝365 by nine\n",
      "removed 优财网 by nine\n",
      "removed 宜信 by nine\n",
      "removed Ｅ镑客 by nine\n",
      "removed 好e贷 by nine\n",
      "removed 工银金融 by nine\n",
      "removed 众可贷 by nine\n",
      "removed 链得得 by nine\n",
      "removed 网络贷 by nine\n",
      "removed 及贷 by nine\n",
      "removed 财付通 by nine\n",
      "removed 宜信 by nine\n",
      "removed 超爱财 by nine\n",
      "removed fomo3d by nine\n",
      "removed 米咖网 by nine\n",
      "removed 高新投 by nine\n",
      "removed 洋钱罐 by nine\n",
      "removed 华金融 by nine\n",
      "removed 乐金所 by nine\n",
      "removed 宜信 by nine\n",
      "removed 滴水贷 by nine\n",
      "removed 贵金属 by nine\n",
      "removed 金道 by nine\n",
      "removed 盒子支付 by nine\n",
      "removed 365金融 by nine\n",
      "removed 联安贷 by nine\n",
      "removed 众可贷 by nine\n",
      "removed 诺诺镑客 by nine\n",
      "removed 多米 by nine\n",
      "removed 普顿 by nine\n",
      "removed 你我金融 by nine\n",
      "removed 中泰证券 by nine\n",
      "removed 中泰国际 by nine\n",
      "removed 汇付天下 by nine\n",
      "removed 投客网 by nine\n",
      "removed 币圈 by nine\n",
      "removed 众可贷 by nine\n",
      "removed 浙商银行 by nine\n",
      "removed 宜信 by nine\n",
      "removed 东方财富 by nine\n",
      "removed 上海骏合金融信息服务有限公司 by nine\n",
      "removed kci by nine\n",
      "removed 大宗商品交易 by nine\n",
      "removed kci by nine\n",
      "removed 借贷宝 by nine\n",
      "removed 君安湘 by nine\n",
      "removed 宜信 by nine\n",
      "removed 财多多 by nine\n",
      "removed 大大宝 by nine\n",
      "removed 芒果金融 by nine\n",
      "removed 乐金所 by nine\n",
      "removed 徽商银行 by nine\n",
      "removed 和耕传承基金 by nine\n",
      "removed 速贷网 by nine\n",
      "removed 海南如意岛公司 by nine\n",
      "removed 云金融 by nine\n",
      "removed MG by nine\n",
      "removed 优财网 by nine\n",
      "removed 火币 by nine\n",
      "removed fomo3d by nine\n",
      "removed 杭州满溢网络科技有限公司 by nine\n",
      "removed 京东支付 by nine\n",
      "removed 云端金融 by nine\n",
      "removed 优财网 by nine\n",
      "removed 一点通 by nine\n",
      "removed 工银金融 by nine\n",
      "removed 众可贷 by nine\n",
      "removed 网乐贷 by nine\n",
      "removed 懒财宝 by nine\n",
      "removed 凑份子 by nine\n",
      "removed fomo3d by nine\n",
      "removed 万国资本 by nine\n",
      "removed 大钱 by nine\n",
      "removed 中国华融 by nine\n",
      "removed 普惠金融 by nine\n",
      "removed 罗麦科技 by nine\n",
      "removed 招联金融 by nine\n",
      "removed 钱包 by nine\n",
      "removed 惠农贷 by nine\n",
      "removed 借贷宝 by nine\n",
      "removed 小花钱包 by nine\n",
      "removed 小额贷 by nine\n",
      "removed 余额宝 by nine\n",
      "removed 华融信 by nine\n",
      "removed 中国人寿 by nine\n",
      "removed 民生投资 by nine\n",
      "removed kci by nine\n",
      "removed 诺诺镑客 by nine\n",
      "removed 余额宝 by nine\n",
      "removed 宜信 by nine\n",
      "removed 小额贷 by nine\n",
      "removed 超爱财 by nine\n",
      "removed 盒子支付 by nine\n"
     ]
    }
   ],
   "source": [
    "rs_df['key_entity'] = rs_df['key_entity'].map(lambda x: trans_keys(trans_map,x)).map(remove_short_entity_by_long).map(remove_nine)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_df.to_csv('evaluation/tmp/multiclass_20191006.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_df['key_entity'] = 'ssss'\n",
    "rs_df.to_csv('evaluation/tmp/entity1.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9728144750000001"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.38912579000 /0.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9390498833333333"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(0.95255572000 - 0.38912579000)/0.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.964390925"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.38575637000  / 0.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.924355"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(0.94036937000 - 0.38575637000) /0.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "178.04537499999995"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5000 * (1-0.964390925)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.924355"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(0.94036937000 - 0.38575637000)/0.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_df.loc[rs_df['key_entity'].map(lambda x:str(x).strip()==''),'negative'] =0\n",
    "rs_df.loc[rs_df['key_entity'].map(lambda x:str(x).strip()==''),'key_entity'] =np.nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_df.to_csv('evaluation/tmp/entity3_20191004.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>negative</th>\n",
       "      <th>key_entity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [id, negative, key_entity]\n",
       "Index: []"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "space_sub = rs_df[rs_df['key_entity'].map(lambda x:str(x).strip()=='')]\n",
    "space_sub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>negative</th>\n",
       "      <th>predict</th>\n",
       "      <th>entity_list</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>790</td>\n",
       "      <td>e9abc6b0</td>\n",
       "      <td>1</td>\n",
       "      <td>[0, 0, 0]</td>\n",
       "      <td>['蚂蚁金服', '花呗', '大学生贷']</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           id  negative    predict             entity_list\n",
       "790  e9abc6b0         1  [0, 0, 0]  ['蚂蚁金服', '花呗', '大学生贷']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_df[raw_df['id']=='e9abc6b0']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  以下是对结果的分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_df = load_basic_dataset(split ='test')\n",
    "raw_df = load_model_rs(model_name='BertSentiEntity',version_id=1)\n",
    "raw_rs_df = reduce_rs_by_id(raw_df)\n",
    "raw_rs_df['key_entity'] = raw_rs_df['key_entity'].map(remove_short_entity_by_long)\n",
    "a = raw_rs_df[raw_rs_df.id.isin(space_sub['id'].values)].sort_values('id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = test_df[test_df.id.isin(space_sub['id'].values)].sort_values('id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "37"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "len(torch.Tensor([1,2,3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.Tensor([1,2,3])\n",
    "b = torch.Tensor([1,0,3])\n",
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dsdc\n",
      "yes\n"
     ]
    }
   ],
   "source": [
    "\n",
    "class TestDataset(Dataset):\n",
    "    def __init__(self, df, max_len=300):\n",
    "        self.x = list(self.make_samples(df, max_len))\n",
    "        self.len = len(self.samples)\n",
    "\n",
    "    def make_samples(self, df, max_len):\n",
    "        texts = df['text'].values\n",
    "        tiltles = df['title'].values\n",
    "        estrs = df['entity'].values\n",
    "        ids = df['id'].values\n",
    "        for i, estr in enumerate(estrs):\n",
    "            for e in estr.split(';'):\n",
    "                yield TextEntitySample(ids[i], texts[i], tiltles[i], e, max_len)\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        return self.samples[index]\n",
    "\n",
    "    def __len__(self):\n",
    "        return self.len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.sum(torch.LongTensor([False,  True, False, False,  True, False])). /1000.0"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
